package com.example.easyscript.controller.task;

import com.example.easyscript.entity.task.Entrance;
import com.example.easyscript.service.task.EntranceService;
import com.example.easyscript.service.task.TaskLoader;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author huzixian
 * @date 2023-03-08
 */
@Api(value = "EntranceController", tags = "")
@Slf4j
@RestController
@RequestMapping(value = "/web/entrance")
public class EntranceController {

    @Resource
    private EntranceService entranceService;

    @ApiOperation("获取对应任务的状态集")
    @GetMapping("/getList/{taskId}")
    public List<Entrance> getList(@PathVariable Integer taskId) {
        return TaskLoader.loadEntityListByTaskId(taskId, entranceService, Entrance.class);
    }

    @ApiOperation("保存")
    @PostMapping("/save")
    public void saveEntrance(@RequestBody Entrance entrance) {
        Integer order = TaskLoader.getMaxOrder(entranceService, entrance.getTaskId());
        entrance.setOrderId(order + 1);
        entranceService.save(entrance);
    }

    @ApiOperation("更新")
    @PostMapping("/update")
    public void updateEntrance(@RequestBody Entrance entrance) {
        entranceService.updateById(entrance);
    }

    @PostMapping("/deletedById/{id}")
    public void deletedById(@PathVariable Integer id) {
        entranceService.removeById(id);
    }
}